﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEngine.Rendering;

namespace Assets.Scripts.System
{
    /// <summary>
    /// SoundSystem 音乐播放管理系统
    /// audioSourceList 0号用于背景音乐BGM播放 1-N用于普通的音乐播放
    /// initAudioSourceCount 表示生成除0号外的数量
    /// </summary>
    public class SoundSystem : MonoSingleton<SoundSystem>, IBaseSystem
    {
        //public float bgmVolume = 1.0f;
        //public float soundVolume = 1.0f;
        public string bgmAudioClipID = "";
        [Range(1, 100)]
        public int initAudioSourceCount = 5;
        public int curAudioSourceIndex = 1;
        public List<AudioSource> audioSourceList = new List<AudioSource>();
        public List<AudioClipInfo> audioClipInfoList = new List<AudioClipInfo>();

        private Dictionary<string, AudioClipInfo> audioClipInfoDict = new Dictionary<string, AudioClipInfo>();
        private StorageSystem curStorageSystem;
        private StorageSystem CurStorageSystem
        {
            get
            {
                if(curStorageSystem == null)
                {
                    curStorageSystem = StorageSystem.Instance;
                }
                return curStorageSystem;
            }
            set
            {
                curStorageSystem = value;
            }
        }
        public void Open()
        {
            foreach (var item in audioClipInfoList)
            {
                if (!audioClipInfoDict.ContainsKey(item.audioClipID))
                {
                    audioClipInfoDict.Add(item.audioClipID, item);
                }
            }
            for (int i = 0; i <= initAudioSourceCount; i++)
            {
                AudioSource audioSource = gameObject.AddComponent<AudioSource>();
                audioSource.playOnAwake = false;
                if (i == 0)
                {
                    audioSource.loop = true;
                    audioSource.volume = CurStorageSystem.storageInfo.BGMVol;
                    if (bgmAudioClipID != "")
                    {
                        PlayBGM(bgmAudioClipID);
                    }
                }
                else
                {
                    audioSource.volume = CurStorageSystem.storageInfo.SoundVol;
                }
                audioSourceList.Add(audioSource);
            }
        }
        public void Close()
        {
            foreach (var item in audioSourceList)
            {
                Destroy(item);
            }
            audioSourceList.Clear();
        }

        public void OnUpdate()
        {

        }
        /// <summary>
        /// 设置BGM音量
        /// </summary>
        /// <param name="vol"></param>
        public void SetBGMVolume(float vol)
        {
            CurStorageSystem.storageInfo.BGMVol = vol;
            if (audioSourceList.Count > 0)
            {
                audioSourceList[0].volume = CurStorageSystem.storageInfo.BGMVol;
            }
        }
        /// <summary>
        /// 设置音效音量
        /// </summary>
        /// <param name="vol"></param>
        public void SetSoundVolume(float vol)
        {
            CurStorageSystem.storageInfo.SoundVol = vol;
            for (int i = 1; i < audioSourceList.Count; i++)
            {
                audioSourceList[i].volume = CurStorageSystem.storageInfo.SoundVol;
            }
        }
        /// <summary>
        /// 播放BGM
        /// </summary>
        /// <param name="audioClipID"></param>
        /// <param name="vol"></param>
        public void PlayBGM(string audioClipID, float vol = -1)
        {
            if (audioClipInfoDict.ContainsKey(audioClipID))
            {
                AudioSource bgmAudioSource = audioSourceList[0];
                if (bgmAudioSource.clip == audioClipInfoDict[audioClipID].audioClip) return;
                bgmAudioSource.clip = audioClipInfoDict[audioClipID].audioClip;
                if (vol != -1)
                {
                    bgmAudioSource.volume = vol;
                }
                bgmAudioSource.Play();
            }
        }
        /// <summary>
        /// 现在有无BGM在播
        /// </summary>
        /// <returns></returns>
        public bool IsPlayingBGM()
        {
            AudioSource bgmAudioSource = audioSourceList[0];
            return bgmAudioSource.isPlaying;
        }

        /// <summary>
        /// 播放一个音乐，占用播放器
        /// </summary>
        /// <param name="audioClipID"></param>
        /// <param name="loop"></param>
        /// <param name="vol"></param>
        /// <returns>播放器的索引值，可用于停止播放</returns>
        public int PlaySound(string audioClipID, bool loop = false, float vol = -1)
        {
            if (audioClipInfoDict.ContainsKey(audioClipID))
            {
                AudioSource audioSource = null;
                for (int i = 0; i < 5; i++)
                {
                    int index = curAudioSourceIndex + i;
                    if (index >= initAudioSourceCount + 1)
                    {
                        index -= initAudioSourceCount;
                    }
                    if (!audioSourceList[index].isPlaying)
                    {
                        audioSource = audioSourceList[index];
                        curAudioSourceIndex = index;
                        break;
                    }
                }
                if (audioSource == null)
                {
                    int index = ++curAudioSourceIndex;
                    if (index >= initAudioSourceCount + 1)
                    {
                        index -= initAudioSourceCount;
                    }
                    audioSource = audioSourceList[index];
                    curAudioSourceIndex = index;
                }
                AudioClip audioClip = audioClipInfoDict[audioClipID].audioClip;
                if (vol != -1)
                {
                    audioSource.volume = vol;
                }
                audioSource.loop = loop;
                audioSource.clip = audioClip;
                audioSource.Play();
                return curAudioSourceIndex;
            }
            return -1;
        }
        /// <summary>
        /// 额外播放一个2D音乐，不占用播放器
        /// </summary>
        /// <param name="audioClipID"></param>
        /// <param name="volScale">播放器音量的缩放值</param>
        public void PlayOneShot(string audioClipID, float volScale = 1)
        {
            if (audioClipInfoDict.ContainsKey(audioClipID))
            {
                AudioSource audioSource = audioSourceList[0];
                AudioClip audioClip = audioClipInfoDict[audioClipID].audioClip;
                audioSource.PlayOneShot(audioClip, volScale);
            }
        }
        /// <summary>
        /// 在特定位置播放3D音乐
        /// </summary>
        /// <param name="audioClipID"></param>
        /// <param name="pos"></param>
        /// <param name="vol"></param>
        public void PlayClipAtPoint(string audioClipID, Vector3 worldPos, float vol = -1)
        {
            if (audioClipInfoDict.ContainsKey(audioClipID))
            {
                AudioClip audioClip = audioClipInfoDict[audioClipID].audioClip;
                if (vol != -1)
                {
                    AudioSource.PlayClipAtPoint(audioClip, worldPos, vol);
                }
                else
                {
                    AudioSource.PlayClipAtPoint(audioClip, worldPos);
                }
            }
        }
        /// <summary>
        /// 停止指定播放器的播放
        /// </summary>
        /// <param name="audioSourceIndex"></param>
        public void StopSound(int audioSourceIndex)
        {
            if (audioSourceList.Count > audioSourceIndex)
            {
                AudioSource audioSource = audioSourceList[audioSourceIndex];
                if (audioSource.isPlaying)
                {
                    audioSource.Stop();
                }
            }
        }
    }
    [Serializable]
    public class AudioClipInfo
    {
        public string audioClipID;
        public string audioClipName;
        public AudioClip audioClip;
    }
}